<template>
  <el-table :data="get_menuList" border style="width: 100%"  row-key="id" default-expand-all>
    <el-table-column prop="id" label="菜单编号" width="180"> </el-table-column>
    <el-table-column prop="title" label="菜单名称" width="180"> </el-table-column>
    <el-table-column prop="pid" label="上级菜单"> </el-table-column>
    <el-table-column prop="icon" label="菜单图标"> </el-table-column>
    <el-table-column prop="url" label="菜单地址"> </el-table-column>
    <el-table-column prop="status" label="状态">
     <!-- 封装好的组件，无法获取数据，如果要使用数据，得通过作用域插槽 -->
      <template v-slot="item">
        <el-button type="primary" size='small' plain v-if="item.row.status == 1">启用</el-button>  
        <el-button type="danger"  size='small' plain v-else>禁用</el-button> 
      </template>
       
    </el-table-column>
    <el-table-column prop="address" label="操作"> 
      <template v-slot="item">
        <el-button type="primary" size='small' @click="editItem(item.row.id)">编辑</el-button>
        <el-button type="danger" size='small' @click='del(item.row.id)'>删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>



  <script>
  // 引入辅助性函数
  import {mapGetters,mapActions} from "vuex"
  import {menudelete} from "@/request/api.js"
export default {
  data() {
    return {
    };
  },
  methods:{
    ...mapActions({
      action_menuList:"menu/action_menuList"
    }),
    //1.点击编辑按钮，显示编辑弹框
    editItem(id){
        //通知父组件，显示弹框
        this.$emit("edit",id)
    },
    //2.点击删除按钮，删除一项
    del(id){
      //a.询问是否真的删除
      this.$confirm('此操作将永久删除该项, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          //删除 -- 发起请求，删除数据库中的数据
          let res = await menudelete({id});
          if(res.code == 200){
            this.$message.success(res.msg);
            //更新页面，重新获取最新的数据的数据
            this.action_menuList();
          }else{
            this.$message.warning(res.msg)
          }

        }).catch(() => {
          this.$message.info("已取消删除"); 
        });
    }
  },
  //计算属性
  computed:{
    ...mapGetters({
      get_menuList:"menu/get_menuList"
    })
  },

  //挂载完成
  mounted(){
     //请求表格数据
     this.action_menuList();
  }
};
</script>